<?php

//------------------------
// 后台订单控制器
//-------------------------
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Order as OrderModel;
use think\Config;
use think\Db;
use constant\OrderStatus;
use constant\PayStatus;
use constant\ShippingStatus;

class Order extends AdminBase
{
    protected $OrderModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->OrderModel       = new OrderModel();
    }


    // 列表
    public function index(){
        $order_sn        = $this->request->param('order_sn','','trim');
        $order_status    = $this->request->param('order_status',0,'intval');
        $pay_status      = $this->request->param('pay_status',0,'intval');
        $shipping_status = $this->request->param('shipping_status',0,'intval');
        $pay_name        = $this->request->param('pay_name','','trim');
        $start           = $this->request->param('start','','trim');
        $stop            = $this->request->param('stop','','trim');

        $where = array();
        if ($order_sn) {
             $where['order_sn'] = array('like','%'.$order_sn.'%');
        }
        if ($order_status) {
            $where['order_status'] = array('eq',$order_status);
        }
        if ($pay_status) {
            $where['pay_status'] = array('eq',$pay_status);
        }
        if ($shipping_status) {
            $where['shipping_status'] = array('eq',$shipping_status);
        }
        if ($pay_name) {
            $where['pay_name'] = array('eq',$pay_name);
        }
        //日期搜索
        if ($start) {
            $_start = strtotime(date('Y-m-d 00:00:00', strtotime($start)));
            $where['add_time'] = array('egt',$_start);
        }
        if ($stop) {
            $_stop  = strtotime(date('Y-m-d 23:59:59', strtotime($stop)));
            $where['add_time'] = array('elt',$_stop);
        }
        if ($start && $stop) {
            $_start = strtotime(date('Y-m-d 00:00:00', strtotime($start)));
            $_stop  = strtotime(date('Y-m-d 23:59:59', strtotime($stop)));
            if ($_start > $_stop) {
                $_stop = $_start + 86400;
            }
            $where['add_time'] = array('between',array($_start,$_stop));
        }

        $_where = my_serialize($where);//搜索条件序列化，导出用
        $this->assign('_where', $_where);

        $info = $this->OrderModel->where($where)->
          paginate(10,false,['query' => [
            'order_status'    =>$order_status,
            'pay_status'      =>$pay_status,
            'shipping_status' =>$shipping_status,
            'pay_name'        =>$pay_name,
            'start'           =>$start,
            'stop'            =>$stop,
            'order_sn'        =>$order_sn
            ] ]);

        $this->assign('order_sn',$order_sn);
        $this->assign('order_status',$order_status);
        $this->assign('pay_status',$pay_status);
        $this->assign('shipping_status',$shipping_status);
        $this->assign('pay_name',$pay_name);
        $this->assign('start',$start);
        $this->assign('stop',$stop);
        $this->assign('info',$info);

        return $this->fetch('index');
    }

        // 删除操作
        public function delete(){
            $id = $this->request->param('id',0,'intval');

            $info = $this->OrderModel->where('id',$id)->field('pay_status')->find()->toArray();

            if ($info['pay_status'] > 1) {
                $this->error('订单已支付，不能删除');
            }

            $where['id'] = array('eq',$id);

            $res = $this->OrderModel->where($where)->delete();

            if ($res === false) {
                $this->error($this->OrderModel->getError());
            } else{
                // 删除成功后去删除对应的订单商品
                Db('OrderGoods')->where('goods_id',$id)->delete();
                $this->success('删除成功');
            }
        }

        // 详情
        public function detail(){
            $id = $this->request->param('id',0,'intval');

            $orderInfo = $this->OrderModel->where('id',$id)->find()->toArray();
            $this->assign('orderInfo',$orderInfo);

            $goodsInfo = Db('OrderGoods')->where('order_id',$orderInfo['id'])->select()->toArray();

            foreach ($goodsInfo as $k => $v) {
                $res = Db('goods')->where('id',$v['goods_id'])->field('goods_name,goods_image')->find();
                $goodsInfo[$k]['goods_name'] = $res['goods_name'];
                $goodsInfo[$k]['goods_image'] = $res['goods_image'];
            }


            $this->assign('goodsInfo',$goodsInfo);
            // halt($goodsInfo);

            return $this->fetch('detail');
        }

        // 价格微调
        public function trimPrice(){
            $data = $this->request->param('');

            $info = $this->OrderModel->where('id',$data['order_id'])->field('pay_status,shipping_price,total_amount')->find()->toArray();

            if ($info['pay_status'] > 1) {
                $this->error('订单已支付，不能微调价格');
            }

            $validate_result = $this->validate($data, 'Order.trimPrice');

            if ($validate_result !== true) {
                    $this->error($validate_result);
            }

            $fine_tune_price = (float)$data['fine_tune_price'];

            $actual_price = (float)$info['shipping_price'] + (float)$info['total_amount'] + $fine_tune_price;

            $editData = array(
                'fine_tune_price'   => $fine_tune_price,
                'actual_price'      => $actual_price
            );


            $res = $this->OrderModel->where('id',$data['order_id'])->update($editData);

            if ($res === false) {
                $this->error($this->OrderModel->getError());
            } else {
                $this->success('价格微调成功');
            }
        }

        // 发货页面
        public function deliverGoodsPage(){
            $order_id = $this->request->param('order_id',0,'intval');
            if (!$order_id) {
                $this->error('非法操作');
            }
            $this->assign('order_id',$order_id);

            $shippingInfo = Db('Shipping')->field('id,shipping_name')->select()->toArray();
            $this->assign('shippingInfo',$shippingInfo);

            return $this->fetch('deliver_goods');
        }

        // 发货编辑页面
        public function editdeliver(){
            $order_id = $this->request->param('order_id',0,'intval');
            if (!$order_id) {
                $this->error('非法操作');
            }
            $this->assign('order_id',$order_id);

            $deliverInfo = Db('Order')->where('id',$order_id)->field('shipping_name,deliver_sn')->find();

            $this->assign('deliverInfo',$deliverInfo);

            $shippingInfo = Db('Shipping')->field('id,shipping_name')->select()->toArray();
            $this->assign('shippingInfo',$shippingInfo);

            return $this->fetch('deliver_goods_edit');
        }

        // 发货操作
        public function deliverGoodsProc(){
            $data = $this->request->param('');

            $validate_result = $this->validate($data, 'Order.deliverGoods');

            if ($validate_result !== true) {
                    $this->error($validate_result);
            }

            $shippingInfo = Db('shipping')->where('id',$data['shipping_id'])->field('shipping_name,shipping_code')->find();

            $editData = array(
                'shipping_status'   => ShippingStatus::SHIPPED,
                'deliver_sn'        => $data['deliver_sn'],
                'shipping_name'      => $shippingInfo['shipping_name'],
                'shipping_code'      => $shippingInfo['shipping_code']
            );


            $res = $this->OrderModel->where('id',$data['order_id'])->update($editData);

            if ($res === false) {
                $this->error($this->OrderModel->getError());
            } else {
                $this->success('发货成功');
            }
        }

        // 订单导出
        public function exportOrder(){
            $act = 'order_list';

            action('common/Excel/export',array('act' => $act));
        }





}